//
//  CJStatusViewModel.swift
//  SinaWeibo
//
//  Created by 程建 on 16/9/27.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class CJStatusViewModel: NSObject {
    
    //通过一个存储型属性 给cell提供一个直接的数据
    var avatarImage:UIImage?
    
    var memberImage:UIImage?
    
    var iconURL:URL?
    
    //评论的文字
    var comment_text:String?
    
    //转发的文字
    var repost_text:String?
    
    //点赞的文字
    var ohYeahText:String?
    
    
    //缩略图
    var pictureInfos:[CJStatusPictureInfo]? {
        
        return status?.retweeted_status == nil ? status?.pic_urls:status?.retweeted_status?.pic_urls
    }
    
    var sourceText:String?
    
    
    var status:CJStatus? {
        
        didSet{
            dealAvatarImage()
            
            dealMemberImage()
            
            dealHeadURL()
            
            //评论的Int 转化为需要的Sting类型的
            comment_text = dealToolBarText(count: status?.comments_count ?? 0, defaultText: "评论")
            
            //转发
            repost_text = dealToolBarText(count: status?.reposts_count ?? 0, defaultText: "转发")
            
            //点赞的文字
            ohYeahText = dealToolBarText(count: status?.ohYeah_count ?? 0, defaultText: "赞")
            
            sourceText = dealSource()
        }
    
    }
    
    
    //微博发布时间
    //计算型属性
    var timeText: String? {
        
        //获取微博发布时间
        let dateString = status?.created_at ?? ""
        
        //1.实例化日期格式对象
        //let dateFormatter = DateFormatter()
        //2.对象字符串设置格式符
        sharedDateFormater.dateFormat = "EEE MMM dd HH:mm:ss zzz yyyy"
        //2.1 设置日期本地化信息,真机上转化会失败
        //设置日期本地化信息是根据字符串中包含的信息来设置
        sharedDateFormater.locale = Locale(identifier: "en")
        //3.将日期字符串转化为 日期Date对象
        let creatDate = sharedDateFormater.date(from: dateString)
        
        //如果创建的日期转换不成工就直接return一个空串
        guard let sinaDate = creatDate else {
            return ""
        }
        
        
        let calendar = Calendar.current
        
        //判断日期
        if isDateInThisYear(targetDate: sinaDate) {
            if calendar.isDateInToday(sinaDate) {
                //创建当前日期对象
                let date = Date()
                let detla = date.timeIntervalSince(sinaDate)
                if detla < 60 {
                    return "刚刚"
                }else if detla < 3600 {
                    //分钟前,去掉小数点部分
                    return "\(Int(detla) / 60)分钟前"
                }else{
                    //小时前
                    return "\(Int(detla) / 3600)小时前"
                }
            }else if calendar.isDateInYesterday(sinaDate) {
                
                //昨天
                sharedDateFormater.dateFormat = "昨天 HH:mm"
                return sharedDateFormater.string(from: sinaDate)
                
            }else {
            
                //今年的其他时间
                sharedDateFormater.dateFormat = "MM-dd"
                return sharedDateFormater.string(from: sinaDate)
            }
        }else {
            
            //非今年
            sharedDateFormater.dateFormat = "yyyy-MM-dd"
            return sharedDateFormater.string(from: sinaDate)
            
        }
    }
    
    private func isDateInThisYear(targetDate:Date)->Bool {
        
        //获取本地时间
        let currentDate = Date()
        //重新设置日期格式化对象
        sharedDateFormater.dateFormat = "yyyy"
        //获取创建的年份
        let creatYear = sharedDateFormater.string(from: targetDate)
        //获取当前的年份
        let currentYear = sharedDateFormater.string(from: currentDate)
        
        //返回比较结果
        return creatYear == currentYear
        
    }
    
    //微博来源
    private func dealSource()->String {
        //"<a href=\"http://app.weibo.com/t/feed/6vtZb0\" rel=\"nofollow\">微博 weibo.com</a>"
        
        //获取微博来源的字符串
        let str = status?.source ?? ""
        //定义开始标志
        let startFlag = "\">"
        //定义结束标志
        let endFlag = "</a>"
        
        //查找标志所在的位置
        guard let startRangeIndex = str.range(of: startFlag), let endRangeIndex = str.range(of: endFlag) else {
            return "来自火星"
        }
        
        //获取拼接的范围
        //upperBound 从范围的后面开始
        let startIndex = startRangeIndex.upperBound
        //lowerBound 从范围的前面开始
        let endIndex = endRangeIndex.lowerBound
        
        let rangeIndex = startIndex..<endIndex
        
        //截取字符串
        let subStr = str.substring(with: rangeIndex)
        
        return subStr
    }
    
    
    //处理底部工作条显示的文字
    //1.如果有评论就先显示评论了多少条
    //2.如果没有就显示默认的
    //3.如果超出就如12345条 -->就显示1.2万
    
    func dealToolBarText(count:Int,defaultText:String) -> String{
    
        if count == 0 {
            
            return defaultText
            
        }
        if count > 10000{
        
            return "\(Double(count/1000)/10)万"
        }
        
        
        return count.description
    }
    
    
    
    
    
    private func dealHeadURL() {
    
        let urlString = status?.user?.avatar_large ?? ""
        
        iconURL = URL(string: urlString)
    }
    
    
    //cell不能直接显示数据,都可以直接在viewMode中处理
    private func dealAvatarImage() {
        
        let type = status?.user?.verified_type ?? -1
        
        
        switch type {
        case 0:
            avatarImage = #imageLiteral(resourceName: "avatar_vip")
        case 2,3,5:
            avatarImage = #imageLiteral(resourceName: "avatar_enterprise_vip")
        case 220:
            avatarImage = #imageLiteral(resourceName: "avatar_grassroot")
        default:
            avatarImage = nil
        }
    
    }
    
    private func dealMemberImage() {
        
        //在if let/var 语句中赋值对象后面跟 ',' ','就可以直接使用前面赋值的对象
        if let mbrank = status?.user?.mbrank, mbrank > 0 && mbrank < 7 {
        
            memberImage = UIImage(named: "common_icon_membership_level\(mbrank)")
        
        }
    
    }
    
    
}
